using System.Collections.Generic;
using UnityEditor.Experimental.GraphView;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.Rendering.Universal;

public class PlayerController :FSM
{
    public static PlayerController Instance;

    
  
    
    
  

    private Dictionary<string,Vector3> skillOffsetDict = new Dictionary<string, Vector3>();
    public override void Awake()
    {
        base.Awake();
        

        Instance = this;

        
        input = new Playerinput();
        input.Player.Move.performed += Move;
        input.Player.Move.canceled += StopMove;
        input.Player.Jump.performed += Jump;
        input.Player.Select.performed += Select;
        input.Player.UnSelect.performed += UnSelect;
        input.Player.SkillUP.performed += SkillUp;
        input.Player.Pause.performed += Pause;
        skillOffsetDict.Add("Bob", new Vector3(-3,3));




        SwitchActionMap(input.Player);
    }

    private void SkillUP_performed(InputAction.CallbackContext obj)
    {
        throw new System.NotImplementedException();
    }

    private void Select_performed(InputAction.CallbackContext obj)
    {
        throw new System.NotImplementedException();
    }

    private void OnDisable()
    {
        input.Disable();
    }
    public override void Update()
    {
        base.Update();
      
        
        
       
    }

   





    public void Move(InputAction.CallbackContext context)//移动相关
    {
        inputDirection = input.Player.Move.ReadValue<Vector2>();
    }
    public void StopMove(InputAction.CallbackContext context) 
    { 
        inputDirection=Vector2.zero;
    }
    public void Jump(InputAction.CallbackContext context)
    {
        isJump = true;
    }
    public void Select(InputAction.CallbackContext context)//选中
    {
        if(hit.collider!= null)
        {
            Debug.Log("选中" + hit.collider.name);
            GameEventsManager.Instance().chosenedEvents.PlayerChosen(hit.collider.name);// 发出该机关被玩家选中事件
            Save.Instance.chosenName = hit.collider.name;// 被选中物品的名字进行公共和存档
            Save.Instance.chosenTag = hit.collider.tag; // 被选中物品的名字进行公共和存档

        }
    }
    public void UnSelect(InputAction.CallbackContext context)//取消选中
    {
        GameEventsManager.Instance().chosenedEvents.PlayerChosen(name);
        Save.Instance.chosenName = name;
    }
    public void SkillUp(InputAction.CallbackContext context)
    {
        GameEventsManager.Instance().statusEvents.ChangeStatus(Save.Instance.chosenName, 1);
        if (Save.Instance.chosenName == name)
        {
            GameObject skill = Instantiate(Resources.Load("Skill/" + Save.Instance.chosenTag), transform) as GameObject;// ����Tag��Resource�ļ����µ�Skill�ļ�����ֱ�ӻ�ȡ��Ӧ��Ԥ����
            skill.transform.position = transform.position + skillOffsetDict[Save.Instance.chosenTag];
            skill.GetComponent<SkillBase>().InitSkill(SkillBase.E_SkillType.Gain);
            skill.GetComponent<SkillBase>().connectRb = rb;
            rb.isKinematic = true;

        }

    }
    public void  SkillDown(InputAction.CallbackContext context) 
    {
        GameEventsManager.Instance().statusEvents.ChangeStatus(Save.Instance.chosenName, -1);
        if (Save.Instance.chosenName == name)
        {
            GameObject skill = Instantiate(Resources.Load("Skill/" + Save.Instance.chosenTag), transform) as GameObject;// ����Tag��Resource�ļ����µ�Skill�ļ�����ֱ�ӻ�ȡ��Ӧ��Ԥ����
            skill.transform.position = transform.position + skillOffsetDict[Save.Instance.chosenTag];
            skill.GetComponent<SkillBase>().InitSkill(SkillBase.E_SkillType.Loss);
            skill.GetComponent<SkillBase>().connectRb = rb;
            rb.isKinematic = true;
        }

    }
    public void Pause(InputAction.CallbackContext context)
    {

        isPause = !isPause;
        GameEventsManager.Instance().pauseEvents.PlayerPause(isPause);

    }



















    private void OnTriggerStay2D(Collider2D collision)//地面检测
    {
        if (collision.gameObject.layer == 6 || collision.gameObject.layer == 7)
        {

            isJump = false;
            isJumping = false;
        }
       

    }
















    public void trapped()
    {
        isTrapped = true;
    } 

}


